﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class EditBuy : BasePage
    {
        private ExcuteDao InformationManage = new ExcuteDao();

        public bool isGiftFlag = false;
        /// <summary>
        /// 页面初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.QueryString["isMager"]))
            {
                this.txtTel.Enabled = false;
                this.txtTel.ReadOnly = true;
                this.txtPhone.Enabled = false;
                this.txtPhone.ReadOnly = true;
                this.txtAdd.Enabled = false;
                this.txtAdd.ReadOnly = true;
                this.ddlListMenu.Enabled = false;
            }

            if (!IsPostBack)
            {
                this.btnSubmit.Visible = false;
                if (!string.IsNullOrEmpty(Request.QueryString["InformationID"]))
                {
                    Session["UrlType"] = "EditInformation";
                    Session["Edit_InformationID"] = SpecialCharHandler.HtmlDiscode(Request.QueryString["InformationID"]).Replace("&amp;", "&");
                    SetMenu();
                    this.btnSubmit.Visible = true;
                    SetInitData();

                }
            }
        }
        private void SetMenu()
        {
            ddlListMenu.Items.Clear();
            //1;已购买；2：已退订；3：已发货；4：已收到；5：已完成。
            ddlListMenu.Items.Add(new ListItem("已购买", "1"));
            ddlListMenu.Items.Add(new ListItem("已退订", "2"));
            ddlListMenu.Items.Add(new ListItem("已发货", "3"));
            ddlListMenu.Items.Add(new ListItem("已收到", "4"));
            ddlListMenu.Items.Add(new ListItem("已完成", "5"));
        }
        /// <summary>
        /// 设置初始化页面信息
        /// </summary>
        private void SetInitData()
        {
            //获取用户信息
            DataTable dtInformation = InformationManage.Get_BuyListByid(Request.QueryString["InformationID"]);
            if (dtInformation.Rows.Count > 0)
            {
                lblTitle.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["listTitle"].ToString());
                lblUserId.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["userId"].ToString());
                txtCusName.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["cusName"].ToString());
                txtTel.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["tel"].ToString());
                txtPhone.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["phone"].ToString());
                txtAdd.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["address"].ToString());
                lblCount.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["buyCount"].ToString());
                lblPoint.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["buyCount"].ToString());
                txtMemo.Text = SpecialCharHandler.HtmlDiscode(dtInformation.Rows[0]["mome"].ToString());

                ddlListMenu.SelectedValue = dtInformation.Rows[0]["status"].ToString();

            }
            //InformationFilter.Information_pwd = dtInformation.Rows[0]["Information_Pwd"].ToString();
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //判断是否保存成功
                bool isOk = false;
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }

                //添加用户信息到数据库中
                if (InformationManage.Update_BuyListALL(
                    Convert.ToInt32(Request.QueryString["InformationID"]),
                    SpecialCharHandler.HtmlEncode(ddlListMenu.SelectedValue),
                    SpecialCharHandler.HtmlEncode(txtMemo.Text),
                    SpecialCharHandler.HtmlEncode(txtTel.Text),
                    SpecialCharHandler.HtmlEncode(txtPhone.Text),
                    SpecialCharHandler.HtmlEncode(txtAdd.Text),
                    SpecialCharHandler.HtmlEncode(txtCusName.Text),
                    DateTime.Now.ToString()) == -1)
                {
                    isOk = true;
                }

                if (isOk)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["isMager"]))
                    {
                        ReturnList("BuyList.aspx?UrlType=buy", "编辑信息成功！");
                    }
                    else
                    {
                        ReturnList("BuyListCus.aspx?UrlType=buy", "编辑信息成功！");
                    }
                }
                else
                {
                    ShowMsg("编辑信息失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }
        }

        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtCusName.Text.TrimEnd()))
            {
                ShowMsg("请收货人姓名！", 1);
                txtCusName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtTel.Text.TrimEnd()))
            {
                ShowMsg("请输入号手机号码！", 1);
                txtTel.Focus();
                return false;
            }
            return true;
        }
    }
}